package com.freak.kotlinfitnessserver.activity.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import com.freak.kotlinfitnessserver.activity.fragment.viewmodel.FeaturesViewModel
import com.freak.kotlinfitnessserver.app.App
import com.freak.kotlinfitnessserver.constant.ByteConstant
import com.freak.kotlinfitnessserver.constant.DeviceConstant
import com.freak.kotlinfitnessserver.databinding.FragmentTargetSettingFeaturesBinding
import com.freak.kotlinfitnessserver.room.FeaturesData
import com.freak.kotlinfitnessserver.util.ToastUtil
import kotlinx.coroutines.launch

class TargetSettingFeaturesFragment : Fragment() {
    private lateinit var binding: FragmentTargetSettingFeaturesBinding

    private lateinit var deviceType: String

    private var featuresData: FeaturesData? = null

    private val featuresViewModel: FeaturesViewModel by activityViewModels {
        FeaturesViewModel.FeaturesViewModelFactory((activity?.application as App).fitnessDeviceDatabase.featuresDataDao())
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentTargetSettingFeaturesBinding.inflate(inflater, container, false)
        initClickListener()
        initData()
        return binding.root
    }

    private fun initData() {
        arguments ?: let {
            return
        }
        deviceType = requireArguments().getString(DeviceConstant.deviceType, "")
        lifecycleScope.launch {
            featuresData = featuresViewModel.getFeaturesData(deviceType)
            featuresData ?: let {
                return@launch
            }
            val flag = featuresData!!.targetSettingFeaturesFlag
            updateTargetSettingFeatures(flag)
        }
    }


    private fun updateTargetSettingFeatures(flag: Int) {
        binding.checkBoxSpeedTargetSettingSupported.isChecked =
            flag and ByteConstant.BYTE_0X01 != 0
        binding.checkBoxInclinationTargetSettingSupported.isChecked =
            flag and ByteConstant.BYTE_0X02 != 0
        binding.checkBoxResistanceTargetSettingSupported.isChecked =
            flag and ByteConstant.BYTE_0X04 != 0
        binding.checkBoxPowerTargetSettingSupported.isChecked =
            flag and ByteConstant.BYTE_0X08 != 0
        binding.checkBoxHeartRateTargetSettingSupported.isChecked =
            flag and ByteConstant.BYTE_0X10 != 0
        binding.checkBoxTargetedExpendedEnergyConfigurationSupported.isChecked =
            flag and ByteConstant.BYTE_0X20 != 0
        binding.checkBoxTargetedStepNumberConfigurationSupported.isChecked =
            flag and ByteConstant.BYTE_0X40 != 0
        binding.checkBoxTargetedStrideNumberConfigurationSupported.isChecked =
            flag and ByteConstant.BYTE_0X80 != 0
        binding.checkBoxTargetedDistanceConfigurationSupported.isChecked =
            flag and ByteConstant.BYTE_0x100 != 0
        binding.checkBoxTargetedTrainingTimeConfigurationSupported.isChecked =
            flag and ByteConstant.BYTE_0x200 != 0
        binding.checkBoxTargetedTimeInTwoHeartRateZonesConfigurationSupported.isChecked =
            flag and ByteConstant.BYTE_0x400 != 0
        binding.checkBoxTargetedTimeInThreeHeartRateZonesConfigurationSupported.isChecked =
            flag and ByteConstant.BYTE_0x800 != 0
        binding.checkBoxTargetedTimeInFiveHeartRateZonesConfigurationSupported.isChecked =
            flag and ByteConstant.BYTE_0x1000 != 0
        binding.checkBoxIndoorBikeSimulationParametersSupported.isChecked =
            flag and ByteConstant.BYTE_0x2000 != 0
        binding.checkBoxWheelCircumferenceConfigurationSupported.isChecked =
            flag and ByteConstant.BYTE_0x4000 != 0
        binding.checkBoxSpinDownControlSupported.isChecked =
            flag and ByteConstant.BYTE_0x8000 != 0
        binding.checkBoxTargetedCadenceConfigurationSupported.isChecked =
            flag and ByteConstant.BYTE_0x10000 != 0
    }


    private fun initClickListener() {
        binding.buttonSaveTargetSetting.setOnClickListener {
            var flag = ByteConstant.BYTE_0X00
            if (binding.checkBoxSpeedTargetSettingSupported.isChecked) {
                flag = flag xor ByteConstant.BYTE_0X01
            }
            if (binding.checkBoxInclinationTargetSettingSupported.isChecked) {
                flag = flag xor ByteConstant.BYTE_0X02
            }
            if (binding.checkBoxResistanceTargetSettingSupported.isChecked) {
                flag = flag xor ByteConstant.BYTE_0X04
            }
            if (binding.checkBoxPowerTargetSettingSupported.isChecked) {
                flag = flag xor ByteConstant.BYTE_0X08
            }
            if (binding.checkBoxHeartRateTargetSettingSupported.isChecked) {
                flag = flag xor ByteConstant.BYTE_0X10
            }
            if (binding.checkBoxTargetedExpendedEnergyConfigurationSupported.isChecked) {
                flag = flag xor ByteConstant.BYTE_0X20
            }
            if (binding.checkBoxTargetedStepNumberConfigurationSupported.isChecked) {
                flag = flag xor ByteConstant.BYTE_0X40
            }
            if (binding.checkBoxTargetedStrideNumberConfigurationSupported.isChecked) {
                flag = flag xor ByteConstant.BYTE_0X80
            }
            if (binding.checkBoxTargetedDistanceConfigurationSupported.isChecked) {
                flag = flag xor ByteConstant.BYTE_0x100
            }
            if (binding.checkBoxTargetedTrainingTimeConfigurationSupported.isChecked) {
                flag = flag xor ByteConstant.BYTE_0x200
            }
            if (binding.checkBoxTargetedTimeInTwoHeartRateZonesConfigurationSupported.isChecked) {
                flag = flag xor ByteConstant.BYTE_0x400
            }
            if (binding.checkBoxTargetedTimeInThreeHeartRateZonesConfigurationSupported.isChecked) {
                flag = flag xor ByteConstant.BYTE_0x800
            }
            if (binding.checkBoxTargetedTimeInFiveHeartRateZonesConfigurationSupported.isChecked) {
                flag = flag xor ByteConstant.BYTE_0x10000
            }
            if (binding.checkBoxIndoorBikeSimulationParametersSupported.isChecked) {
                flag = flag xor ByteConstant.BYTE_0x2000
            }
            if (binding.checkBoxWheelCircumferenceConfigurationSupported.isChecked) {
                flag = flag xor ByteConstant.BYTE_0x4000
            }
            if (binding.checkBoxSpinDownControlSupported.isChecked) {
                flag = flag xor ByteConstant.BYTE_0x8000
            }
            if (binding.checkBoxTargetedCadenceConfigurationSupported.isChecked) {
                flag = flag xor ByteConstant.BYTE_0x10000
            }
            if (flag == 0) {
                ToastUtil.toastShort("请选择支持的能力")
                return@setOnClickListener
            }
            lifecycleScope.launch {
                val featuresData = featuresViewModel.getFeaturesData(deviceType)
                // 保存到数据库
                featuresData ?: let {
                    val insertFeaturesData = FeaturesData(
                        deviceType = deviceType,
                        fitnessMachineFeaturesFlag = flag,
                        targetSettingFeaturesFlag = 0
                    )
                    featuresViewModel.insertFeaturesData(insertFeaturesData)
                    return@launch
                }
                val updateFeaturesData = FeaturesData(
                    id = featuresData.id,
                    deviceType = featuresData.deviceType,
                    fitnessMachineFeaturesFlag = featuresData.fitnessMachineFeaturesFlag,
                    targetSettingFeaturesFlag = flag
                )
                featuresViewModel.updateFeaturesData(updateFeaturesData)
            }

        }
    }


    companion object {
        @JvmStatic
        fun newInstance(deviceType: String) = TargetSettingFeaturesFragment().apply {
            arguments = Bundle().apply {
                putString(DeviceConstant.deviceType, deviceType)
            }
        }
    }
}